package com.china08.yxyapi.api.manage.tec.teaching.lecture.quesgroup;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;

/**
 * 备授课--添加题组--查询目录对应知识点
 * 
 */
@CrossOrigin
@RestController
public class LectureQuesKnowSchController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyQuesKnowRepository yxyQuesKnowRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/quegroup/bookcontent/know/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {

        String bookcontentsId = reqModel.bookcontentsId;

        Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "目录ID为空");

        List<YxyResBookContentsKnoledges> bookContentId = yxyResBookContentsKnoledgesRepository
                .findByBookContentId(bookcontentsId);

        List<Integer> bookContentsKnowsList = new ArrayList<Integer>();
        RespModel model = new RespModel();
        for (YxyResBookContentsKnoledges yxyResBookContentsKnoledges : bookContentId) {
            YxyQuesKnow quesKnow = yxyQuesKnowRepository.findById(yxyResBookContentsKnoledges.getKnowledgeId());
            Integer id = quesKnow.getId();
            bookContentsKnowsList.add(id);
        }
        model.bookContentsKnowsList = bookContentsKnowsList;

        return model;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String bookcontentsId;

    }

    protected static class RespModel extends YxyApiRespModel {
        public List<Integer> bookContentsKnowsList;
    }

}
